package concurrent;

/**
 * @ClassName SynchronizedObjectLock
 * @Author 刘正星
 * @Date 2021/4/13 上午11:57
 * @Description
 */
public class SynchronizedObjectLock implements Runnable {
    static SynchronizedObjectLock instence = new SynchronizedObjectLock();

    @Override
    public void run() {
        // 同步代码块形式——锁为this,两个线程使用的锁是一样的,线程1必须要等到线程0释放了该锁后，才能执行
        synchronized (this) {
            System.out.println("我是线程" + Thread.currentThread().getName());
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "结束");
        }
    }

    public static void main(String[] args) {

        for (int i = 0; i <10 ; i++) {
            Thread t1 = new Thread(instence);
            t1.setName("t"+i);
            t1.start();
        }

    }
}

